package kr.co.skplanet.commute.service;

import java.util.List;

import kr.co.skplanet.commute.dao.MemberDAO;
import kr.co.skplanet.commute.dao.PointDAO;
import kr.co.skplanet.commute.dao.RouteDAO;
import kr.co.skplanet.commute.dao.StopInfoDAO;
import kr.co.skplanet.commute.domain.Member;
import kr.co.skplanet.commute.domain.Point;
import kr.co.skplanet.commute.domain.Route;
import kr.co.skplanet.commute.domain.StopInfo;
import kr.co.skplanet.commute.exception.IllegalAuthKeyException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service(value="busService")
@Transactional 
public class BusService {
	@Autowired
	MemberDAO memberDAO;
	
	@Autowired
	RouteDAO routeDAO;
	
	@Autowired
	PointDAO pointDAO;
	
	@Autowired
	StopInfoDAO stopinfoDAO;
	
	@Transactional(readOnly = true)
	public List<Route> getRouteList(long id) {
		Member info = memberDAO.findUniqByObject("id", id);
		return info.getRoutes();
	}
	@Transactional(readOnly = true)
	public List<Point> getPointList(long id) {
		Route info = routeDAO.findUniqByObject("id", id);
		return info.getPoints();
	}
	@Transactional(readOnly = true)
	public List<StopInfo> getStopInfoList(long pointid) {
		Point sinfo = pointDAO.findUniqByObject("id", pointid);
		return sinfo.getStopinfos();
	}
	
	@Transactional
	public Route saveRoute(String authKey, Route route, Long memberId) throws IllegalAuthKeyException {
		Member member = memberDAO.findUniqByObject("id", memberId);
		
		if(validateAuthKey(member.getId(), authKey))		
			member.getRoutes().add(route);
		route.setMember(member);
		return route;
	}
	@Transactional
	public Point savePoint(String authKey, Point point, Long routeId) throws IllegalAuthKeyException {
		Route route = routeDAO.findUniqByObject("id", routeId);
		
		if(validateAuthKey(route.getMemberId(), authKey))
			route.getPoints().add(point);
		point.setRoute(route);
		return point;
	}
	@Transactional
	public StopInfo saveStopInfo(String authKey, StopInfo stopInfo, Long pointId) throws IllegalAuthKeyException {
		Point point = pointDAO.findUniqByObject("id", pointId);
		
		if(validateAuthKey(point.getMemberId(), authKey))
			point.getStopinfos().add(stopInfo);
		stopInfo.setPoint(point);
		return stopInfo;
	}
	@Transactional
	public void deleteRoute(String authKey, Long routeId) throws IllegalAuthKeyException {
		Route route = routeDAO.findUniqByObject("id", routeId);
		
		if(validateAuthKey(route.getMemberId(), authKey))
			routeDAO.delete(route);
	}
	@Transactional
	public void deletePoint(String authKey, Long pointId) throws IllegalAuthKeyException {
		Point point = pointDAO.findUniqByObject("id", pointId);
		
		if(validateAuthKey(point.getMemberId(), authKey))
			pointDAO.delete(point);
	}
	@Transactional
	public void deleteStopInfo(String authKey, Long stopInfoId) throws IllegalAuthKeyException {
		StopInfo stopInfo = stopinfoDAO.findUniqByObject("id", stopInfoId);
		
		if(validateAuthKey(stopInfo.getMemberId(), authKey))
			stopinfoDAO.delete(stopInfo);
	}
	
	private boolean validateAuthKey(Long id, String authKey) throws NullPointerException, IllegalAuthKeyException {
		Member member = memberDAO.findUniqByObject("id", id);
		if (member == null)
			throw new NullPointerException();
		else if (!authKey.equals(member.getAuthKey())) {
			throw new IllegalAuthKeyException();
		}
		
		return true;
	}
	
}	// PostService
